<template>
  <div>
    <Category :fetchCategoryId="fetchCategoryId" />

    <el-card style="margin: 20px 0">
      <div v-show="scene == 0">
        <!-- 添加 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!c3"
          @click="addAttr"
          >添加属性</el-button
        >

        <!-- 表格 -->
        <el-table border stripe :data="attrArr" style="width: 100%">
          <el-table-column
            align="center"
            label="序号"
            type="index"
            width="80px"
          >
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="140px">
          </el-table-column>
          <el-table-column label="属性值列表" width="width">
            <template slot-scope="{ row, $index }">
              <el-tag
                type="success"
                style="margin: 0 10px"
                :key="attrValue.id"
                v-for="attrValue in row.attrValueList"
                >{{ attrValue.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="140px">
            <template slot-scope="{ row }">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="editHandle(row)"
              ></el-button>
             <el-popconfirm
  confirm-button-text='确定'
  cancel-button-text='取消'
  icon="el-icon-delete"
  icon-color="red"
  title="这是一段内容确定删除吗？"
  @onConfirm="confirm"
>
  <el-button slot="reference" icon="el-icon-delete" size="mini"></el-button>
</el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div v-show="scene == 1">
        <el-form ref="form" inline>
          <el-form-item label="属性名">
            <el-input
              placeholder="属性名"
              v-model="attrForm.attrName"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!attrForm.attrName"
          @click="addAttrValue"
          >添加属性值</el-button
        >
        <el-button @click="scene = 0">取消</el-button>

        <el-table
          border
          :data="attrForm.attrValueList"
          style="width: 100%; margin: 20px 0"
        >
          <el-table-column
            align="center"
            type="index"
            label="序号"
            width="80px"
          >
          </el-table-column>
          <el-table-column label="属性值名称" width="with">
            <template slot-scope="{ row,$index }">
              <el-input
                autofocus
                v-if="row.show"
                size="mini"
                v-model="row.valueName"
                @blur="blurHandle(row)"
                @keyup.native.enter="keyupHandle(row)"
                ref="cur"
              ></el-input>
              <pre v-else @click="attrClick(row,$index)">{{ row.valueName }}</pre>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="140px"> 
<template slot-scope="{$index}">
 
            <el-button @click="attrForm.attrValueList.splice($index,1)" type="primary" size="mini" icon="el-icon-delete"></el-button>
</template>
          </el-table-column>
        </el-table>
        <el-button
          type="primary"
          :disabled="attrForm.attrValueList.length > 0 ? false : true"
          @click="saveAttr"
          >保存</el-button
        >
        <el-button @click="scene = 0">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
// {
//   "attrName": "string",
//   "attrValueList": [
//     {
//       "attrId": 0,
//       "id": 0,
//       "valueName": "string"
//     }
//   ],
//   "categoryId": 0,
//   "categoryLevel": 0,
//   "id": 0
// }
export default {
  name: "Attr",
  data() {
    return {
      c1: "",
      c2: "",
      c3: "",
      attrArr: [],
      scene: 0,
      attrForm: {
        attrName: "",
        attrValueList: [],
        categoryId: "",
        categoryLevel: 3,
      },
    };
  },
  methods: {
    confirm(){
      console.log('sahngchu');
    },
    keyupHandle(row) {
      this.blurHandle(row);
    },
    fetchCategoryId(id, level) {
      // console.log(id, level);
      switch (level) {
        case 1:
          this.c1 = id;
          this.c2 = "";
          this.c3 = "";
          break;
        case 2:
          this.c2 = id;
          this.c3 = "";
          break;
        default:
          this.c3 = id;
          this.attrForm.categoryId = id;
          this.getAttrList();
          break;
      }
    },

    async getAttrList() {
      let res = await this.$http.attr.reqAttr(this.c1, this.c2, this.c3);
      if (res.code == 200) {
        this.attrArr = res.data;
      }
    },

    addAttr() {
      this.scene = 1;
      this.attrForm.attrValueList = [];
      this.attrForm.attrName = "";
      delete this.attrForm.id;
    },
    addAttrValue() {
      this.attrForm.attrValueList.push({
        attrId: undefined,
        valueName: "",
        show: true,
      });

      this.$nextTick(()=>{

        // console.log(this.$refs[index]);
        // this.$refs[this.attrForm.attrValueList.length-1].focus()
console.log(this.$refs);
        this.$refs['cur'].focus()
      })
    },

    async saveAttr() {
      let { attrForm } = this;

      if (
        !attrForm.id &&
        this.attrArr.find((item) => item.attrName == attrForm.attrName)
      ) {
        this.$message("该属性已经存在");
        return;
      }
      // this.attrForm.attrValueList.forEach((item) => {
      //   if (!item.valueName.trim()) {
      //     this.$message("属性值不能为空");
      //     return;
      //   }
      // });

      let isEmpty = this.attrForm.attrValueList.find(
        (item) => item.valueName.trim() == ""
      );
      if (isEmpty) {
        return this.$message("你瞎牙，还有空的呢");
      }

      this.attrForm.attrValueList.forEach((item) => {
        delete item.show;
      });
      try {
        await this.$http.attr.reqSaveAttrINfo(attrForm);
        this.scene = 0;
        this.getAttrList();
        this.$notify({
          title: "成功",
          message: attrForm.id ? "修改成功" : "添加成功",
          type: "success",
        });
      } catch (error) {
        this.$notify({
          title: "成功",
          message: attrForm.id ? "修改失败" : "添加失败",
          type: "error",
        });
      }
    },
    blurHandle(row) {
      if (row.valueName.trim() == "") {
        this.$message("豪哥提示:属性值不能为空的");
        return;
      }

      let repeat = this.attrForm.attrValueList.find((item) => {
        if (item != row) {
          return item.valueName == row.valueName;
        }
      });

      if (repeat) {
        this.$message("豪哥提示:属性值不能重复");
        this.attrForm.attrValueList.splice(
          this.attrForm.attrValueList.indexOf(row),
          1
        );
        return;
      }

      row.show = false;
    },

    attrClick(row,index) {
      row.show = true;
      this.$nextTick(()=>{

        // console.log(this.$refs[index]);
        this.$refs['cur'].focus()
      })
    },

    editHandle(row) {
      this.attrForm = { ...row };
      this.scene = 1;
      // this.attrForm.attrValueList.forEach(item=>{
      //   this.$set(item,'show',false)
      // });

      this.attrForm.attrValueList = this.attrForm.attrValueList.map((item) => {
        return {
          ...item,
          show: false,
        };
      });
    },
  },
};
</script>

<style>
</style>